package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.PageData;
import com.woniu.entity.dto.CollectDTO;
import com.woniu.entity.vo.CollectVO;
import com.woniu.mapper.CollectMapper;
import com.woniu.pojo.Collect;
import com.woniu.service.ICollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaosong
 * @since 2023-04-01
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements ICollectService {

    @Autowired(required = false)
    private CollectMapper collectMapper;

    @Override
    public Collect searchById(Integer id) {

        Collect collect = collectMapper.searchById(id);
        return collect;
    }

    @Override
    public PageData searchHistory(Integer currentPage, Integer pageSize, CollectDTO collectDTO) {
        IPage<CollectVO> page = new Page<>(currentPage, pageSize);
        IPage<CollectVO> searchPage = collectMapper.searchHistory(page, collectDTO);
        PageData pageData = new PageData(searchPage.getTotal(), searchPage.getRecords());
        return pageData;
    }

    @Override
    public PageData searchCollect(Integer currentPage, Integer pageSize, CollectDTO collectDTO) {

        IPage<CollectVO> page = new Page<>(currentPage, pageSize);
        IPage<CollectVO> searchPage = collectMapper.searchCollect(page, collectDTO);
        PageData pageData = new PageData(searchPage.getTotal(), searchPage.getRecords());
        return pageData;
    }

    @Override
    public Boolean revise(Collect collect) {

        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", collect.getUserId());
        queryWrapper.eq("training_id", collect.getTrainingId());
        Collect c = collectMapper.selectOne(queryWrapper);

        collect.setUpdateTime(new Date());

        return c == null ? collectMapper.revise(collect) : false;
    }

    @Override
    public Boolean deletePart(List<Integer> ids) {

        Boolean flag = collectMapper.deletePart(ids);
        return flag;
    }

    @Override
    public Boolean decrease(Integer id) {

        Boolean flag = collectMapper.decrease(id);
        return flag;
    }

    @Override
    public Boolean increase(Collect collect) {

        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", collect.getUserId());
        queryWrapper.eq("training_id", collect.getTrainingId());
        Collect c = collectMapper.selectOne(queryWrapper);

        collect.setCreateTime(new Date());
        collect.setUpdateTime(new Date());

        return c == null ? collectMapper.increase(collect) : false;
    }

    @Override
    public PageData searchPage(Integer currentPage, Integer pageSize, CollectDTO collectDTO) {

        IPage<CollectVO> page = new Page<>(currentPage, pageSize);
        IPage<CollectVO> searchPage = collectMapper.searchPage(page, collectDTO);
        PageData pageData = new PageData(searchPage.getTotal(), searchPage.getRecords());
        return pageData;
    }
}
